﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Coffee_Restaurant.Database;

namespace Coffee_Restaurant.Forms
{
    public partial class ManagerPermission : Form
    {
        public ManagerPermission()
        {
            InitializeComponent();
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void ManagerPermission_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        public void LoadData()
        {
            using (var db = new DatabaseEntities())
            {
                //clear first
                var model = db.Roles.ToList();
                var columnFirst = new DataGridViewTextBoxColumn();
                columnFirst.Name = "FirstColumn";
                columnFirst.ReadOnly = true;
                dataGridView1.Columns.Add(columnFirst);
                dataGridView1.Columns[0].HeaderCell = new DataGridViewColumnHeaderCell() { Style = new DataGridViewCellStyle() { BackColor = System.Drawing.SystemColors.Control, ForeColor = System.Drawing.SystemColors.Control } };
                foreach (var role in model)
                {
                    //khoi tao column
                    var column = new DataGridViewCheckBoxColumn { Name = role.RoleName, HeaderText = role.RoleName };
                    dataGridView1.Columns.Add(column);
                }
                var modelGroup = db.Groups.ToList();
                for (int i = 0; i < modelGroup.Count; i++)
                {
                    var group = modelGroup[i];
                    //khoi tao cell cua rows theo so cot
                    //gan gia tri
                    var objects = new object[dataGridView1.ColumnCount];
                    objects[0] = group.GroupName;
                    for (int j = 1; j < model.Count; j++)
                    {
                        var roles = model[j - 1];
                        //load xem group nay co nhung quyen gi.
                        var count = db.GroupRoles.Count(x => x.GroupId == group.GroupId && x.RoleId == roles.RoleId);
                        if (count > 0)
                        {
                            objects[j] = true;
                        }
                        else
                        {
                            objects[j] = false;
                        }
                    }
                    dataGridView1.Rows.Insert(i, objects);
                }
                dataGridView1.Columns[0].DefaultCellStyle = new DataGridViewCellStyle() { BackColor = System.Drawing.Color.Tan, SelectionBackColor = System.Drawing.Color.Tan };
            }
        }

        private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            dataGridView1.Rows[e.RowIndex].Height = 41;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //label3.Text = dataGridView1.CurrentCell.RowIndex.ToString();
            using (var db = new DatabaseEntities())
            {
                foreach (var row in dataGridView1.Rows)
                {
                    var rowSelect = row as DataGridViewRow;
                    if (rowSelect != null)
                    {
                        //var objects = new object[rowSelect.Cells.Count];
                        for (int i = 0; i < rowSelect.Cells.Count; i++)
                        {
                            //tim kiem phan tu thay doi
                            //objects[i] = valueOfCell;
                            //bo qua i dau tien vi là cột First column bi can
                            if (rowSelect.Cells[i].GetType() == typeof(DataGridViewCheckBoxCell))
                            {
                                var valueOfCell = rowSelect.Cells[i].Value;
                                int rowIndex = rowSelect.Cells[i].RowIndex;
                                int columnIndex = rowSelect.Cells[i].ColumnIndex;
                                var groupName = (string)dataGridView1.Rows[rowIndex].Cells[0].Value;
                                var rolesName = dataGridView1.Columns[columnIndex].HeaderText;
                                var check = db.sp_GroupRoles().FirstOrDefault(x => x.GroupName == groupName && x.RoleName == rolesName);
                                if (check != null && valueOfCell != null && valueOfCell.ToString().ToLower().Equals("false"))
                                {
                                    // false thi xoa trong database
                                    db.GroupRoles.Remove(db.GroupRoles.FirstOrDefault(x => x.GroupRolesId == check.GroupRolesId));
                                    db.SaveChanges();
                                }
                                else if (check == null && valueOfCell != null && valueOfCell.ToString().ToLower().Equals("true"))
                                {
                                    // true thi add neu khong co
                                    var group = db.Groups.FirstOrDefault(g => g.GroupName == groupName);
                                    var roles = db.Roles.FirstOrDefault(r => r.RoleName == rolesName);
                                    if (group != null && roles != null)
                                    {
                                        db.GroupRoles.Add(new GroupRole() { GroupId = group.GroupId, RoleId = roles.RoleId });
                                        db.SaveChanges();
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
